package base;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.ma.BaseListBinding;

import com.ma.R;

import  temp.TempView;

import java.util.ArrayList;
import java.util.List;

import   lomasky.xui.RecyclerOnScrollListener;

/**
 * Created by Administrator on 2016/10/21 0021.
 */

public abstract class BaseListActivity<T> extends BaseActivity {

    public BaseListBinding listBinding;
    public List<T> data = new ArrayList<>();
    RecyclerOnScrollListener recyclerOnScrollListener;

    protected abstract int getLayoutId();

    protected abstract int getViewModelId();

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(this);
    }

    public void setData(@NonNull List<T> list) {
        if (list.isEmpty()) {
            listBinding.setState(TempView.DATA_NULL);
            return;
        } else {
            listBinding.setState(TempView.ISGONE);
        }

        data = list;
        adapter.setData(data);
        adapter.notifyDataSetChanged();


    }

    public void addData(@NonNull List<T> list) {
        if (list.isEmpty()&&adapter.getData().isEmpty()) {
            listBinding.setState(TempView.DATA_NULL);
        } else {
            listBinding.setState(TempView.ISGONE);
            adapter.addAll(list);
        }


    }

    public RecyclerViewBindingAdapter<T> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listBinding = DataBindingUtil.setContentView(this, R.layout.recycler_binding_layout);
        listBinding.setState(TempView.LOADING);
        recyclerOnScrollListener = new RecyclerOnScrollListener() {
            @Override
            public void onLoadNextPage(View view) {
                super.onLoadNextPage(view);
                onLoading();

            }
        };

        listBinding.recyclerView.setLayoutManager(getLayoutManager());
        listBinding.recyclerView.addOnScrollListener(recyclerOnScrollListener);
        adapter = new RecyclerViewBindingAdapter(getViewModelId(), getLayoutId());
        listBinding.recyclerView.setAdapter(adapter);
        listBinding.superFrameLayout.setTempBtClickListener((view, type) -> {

            onRefrash();
        });

    }


    public void onRefrash() {
        if (adapter == null) {
            return;
        }
//
        adapter.clear();
        listBinding.setState(TempView.LOADING);

    }


    public void onLoading() {

    }

    public void setHasMore(boolean hasMore) {
        recyclerOnScrollListener.setHasMore(hasMore);
    }

}
